package com.ganjiangps.huochetou.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ganjiangps.BaseCRUDController;
import com.ganjiangps.huochetou.common.constant.Result_Constant;
import com.ganjiangps.huochetou.model.PersonalManagement;
import com.ganjiangps.huochetou.model.PersonalManagementNo;
import com.ganjiangps.huochetou.model.PersonalManagementNoQuote;
import com.ganjiangps.huochetou.model.PersonalManagementUser;
import com.ganjiangps.huochetou.service.PersonalManagementNoQuoteService;
import com.ganjiangps.huochetou.service.PersonalManagementNoService;
import com.ganjiangps.huochetou.service.PersonalManagementService;
import com.ganjiangps.huochetou.service.PersonalManagementUserService;
import com.github.pagehelper.PageInfo;

/**
 * 大名单匹配小名单
 * 
 * @author xiaoy
 */
@Controller
@RequestMapping("/admin/personalManagementNo")
public class PersonalManagementNoController extends BaseCRUDController<PersonalManagementNo> {

	@Resource
	private PersonalManagementNoService personalManagementNoService;

	@Resource
	private PersonalManagementService personalManagementService;

	@Resource
	private PersonalManagementNoQuoteService personalManagementNoQuoteService;
	@Resource
	private PersonalManagementUserService personalManagementUserService;

	@Override
	protected void preList(Model model, PersonalManagementNo entity, String orderBy) throws IllegalAccessException {
		// 处理分页请求
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		Map<String, Object> map = new HashMap<>();
		initPage(map, pageNum, pageSize);
		List<PersonalManagementNo> lists = personalManagementNoService.listPersonalManagementNo();

		// PageAjax pageAjax = new PageAjax<Entity>(lists);
		PageInfo<Object> pagehelper = initPagehelper(map, lists);
		model.addAttribute("pagehelper", pagehelper);
		model.addAttribute("sf", sf);
		// 回显查询条件
		model.addAttribute("personalManagementNo", entity);
	}

	@Override
	protected void preInsertView(Model model) {
		List<PersonalManagementNo> noList = personalManagementNoService.getPersonalManagementNo();
		model.addAttribute("noList", noList);
	}

	@Override
	protected void preDeleteView(Model model, Long id) {
		PersonalManagementNo personalManagementNo = personalManagementNoService.selectById(id);

		model.addAttribute("personalManagementNo", personalManagementNo);
	}

	@PostMapping("/insertNo")
	@ResponseBody
	public Map<String, Object> insertUsername(String pmno, String no) {
		Map<String, Object> map = new HashMap<>();
		map.put(Result_Constant.RESULT, Result_Constant.FAIL);
		map.put(Result_Constant.MSG, "提示：此小名单编号不存在，添加失败。");
		PersonalManagement personalManagement = personalManagementService
				.selectOne(new EntityWrapper<PersonalManagement>().eq("pmNo", no).eq("pmType", 2));
		if (personalManagement != null) {
			map.put(Result_Constant.MSG, "提示：添加成功。");
			map.put(Result_Constant.RESULT, Result_Constant.SUCCESS);
			map.put("personalManagement", personalManagement);
		}
		return map;
	}

	@Override
	protected void preUpdateView(Model model, Long id) {

		PersonalManagementNo personalManagementNo = personalManagementNoService.selectById(id);
		model.addAttribute("personalManagementNo", personalManagementNo);

		List<PersonalManagementNoQuote> list = personalManagementNoQuoteService.selectList(
				new EntityWrapper<PersonalManagementNoQuote>().eq("parentNo", personalManagementNo.getPmno()));
		model.addAttribute("noList", list);

	}

	@Override
	protected void preDetail(Model model, Long id) {
		PersonalManagementNo personalManagementNo = personalManagementNoService.selectById(id);
		List<PersonalManagementNoQuote> noList = personalManagementNoQuoteService.selectList(
				new EntityWrapper<PersonalManagementNoQuote>().eq("parentNo", personalManagementNo.getPmno()));
		model.addAttribute("personalManagementNo", personalManagementNo);
		model.addAttribute("noList", noList);
	}

	@RequestMapping(value = "userDetail/{id}", method = { RequestMethod.GET, RequestMethod.POST })
	public String userDetail(Model model, @PathVariable("id") Long id) {
		PersonalManagementNo personalManagementNo = personalManagementNoService.selectById(id);
		// 处理分页请求
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		Map<String, Object> map = new HashMap<>();
		initPage(map, pageNum, "3");
		List<PersonalManagementUser> userList = personalManagementUserService.listUsers(personalManagementNo.getPmno());
		PageInfo<Object> pagehelper = initPagehelper(map, userList);
		model.addAttribute("pagehelper", pagehelper);
		// 显示大名单信息
		model.addAttribute("personalManagementNo", personalManagementNo);
		return "/admin/personalmanagementno/personalManagementNo_User_Detail";
	}

	/**
	 * 大名单匹配信息，用户详情
	 */
	@RequestMapping(value = "usersDetail/{id}", method = { RequestMethod.GET, RequestMethod.POST })
	public String usersDetail(Model model, @PathVariable("id") Long id) {
		PersonalManagementNo personalManagementNo = personalManagementNoService.selectById(id);
		// 处理分页请求
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		Map<String, Object> map = new HashMap<>();
		initPage(map, pageNum, "3");
		List<PersonalManagementUser> userList = personalManagementUserService.listUsers(personalManagementNo.getPmno());
		PageInfo<Object> pagehelper = initPagehelper(map, userList);
		model.addAttribute("pagehelper", pagehelper);
		return "/admin/personalmanagementno/personalManagementNo_Users";
	}

}